package it.uniroma2.art.coda.main;

import it.uniroma2.art.coda.exception.FelixInitializationException;
import it.uniroma2.art.owlart.exceptions.ModelAccessException;
import it.uniroma2.art.owlart.exceptions.ModelCreationException;
import it.uniroma2.art.owlart.exceptions.ModelUpdateException;
import it.uniroma2.art.owlart.exceptions.UnsupportedRDFFormatException;
import it.uniroma2.art.owlart.io.RDFFormat;
import it.uniroma2.art.owlart.model.ARTNode;
import it.uniroma2.art.owlart.model.ARTResource;
import it.uniroma2.art.owlart.model.ARTStatement;
import it.uniroma2.art.owlart.model.ARTURIResource;
import it.uniroma2.art.owlart.model.NodeFilters;
import it.uniroma2.art.owlart.models.ModelFactory;
import it.uniroma2.art.owlart.models.OWLArtModelFactory;
import it.uniroma2.art.owlart.models.OWLModel;
import it.uniroma2.art.owlart.models.UnloadableModelConfigurationException;
import it.uniroma2.art.owlart.models.UnsupportedModelConfigurationException;
import it.uniroma2.art.owlart.models.conf.ModelConfiguration;
import it.uniroma2.art.owlart.navigation.ARTResourceIterator;
import it.uniroma2.art.owlart.navigation.ARTStatementIterator;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.sun.org.apache.xml.internal.security.signature.NodeFilter;

public class PrintModelTest {

	
	
	private static String oSGiDirPath="E:/CODATest/OSGiCache";
	
	
	private static String baseUri = "http://art.uniroma2.it";
	
	private static String respositoryDirPath = "E:/CODATest/repDir";
	//private static String importedUri = "http://viaggio";
	private static String importedUri = "http://PersonEmail";
	
	//private static String rdfInputFilePath = "E:/CODATest/rdfFile.owl";
	private static String rdfInputFilePath = "E:/CODATest/PersonEmail.owl";
	
	private static String rdfOutputFilePath = "E:/CODATest/rdfOutputFile.owl";
	
	
	public static void main(String[] args){
		String owlArtModelFactoryImplClassName = "it.uniroma2.art.owlart.sesame2impl.factory.ARTModelFactorySesame2Impl";
		
		try{
	        Class<? extends ModelFactory> owlArtModelFactoryImplClass = (Class<? extends ModelFactory>) 
	        			Class.forName(owlArtModelFactoryImplClassName);
	        OWLArtModelFactory artModelFact = OWLArtModelFactory.createModelFactory(owlArtModelFactoryImplClass.newInstance());
	        String mcClassName = "it.uniroma2.art.owlart.sesame2impl.models.conf.Sesame2PersistentInMemoryModelConfiguration";
	        Class mcClass = Class.forName(mcClassName);
	        ModelConfiguration modelConf = artModelFact.createModelConfigurationObject(mcClass);
	        OWLModel owlModel = artModelFact.loadOWLModel(baseUri, respositoryDirPath, modelConf);
			owlModel.setBaseURI(baseUri);
			owlModel.setDefaultNamespace(baseUri+"#");
			owlModel.clearRDF();
			//owlModel.addRDF(new File(rdfInputFilePath), importedUri, RDFFormat.RDFXML,  NodeFilters.MAINGRAPH);
			owlModel.addRDF(new URL("http://xmlns.com/foaf/spec/20100809.rdf"), importedUri, RDFFormat.RDFXML, NodeFilters.MAINGRAPH);
			//owlModel.addRDF(new URL("http://xmlns.com/foaf/0.1/"), importedUri, RDFFormat.RDFXML, NodeFilters.MAINGRAPH);
			
			
			//"http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
			
			ARTStatementIterator artIter = owlModel.listStatements(NodeFilters.ANY, NodeFilters.ANY, NodeFilters.ANY, true, NodeFilters.ANY); // check last parameter
			
			System.out.println("listing all the triple"); 
			while(artIter.hasNext()){
				ARTStatement artStatement = artIter.next();
				
				ARTResource subj = artStatement.getSubject();
				ARTResource pred = artStatement.getPredicate();
				ARTNode obj = artStatement.getObject();
				
				System.out.println("\n"+subj.toString()); 
				System.out.println("\t"+pred.toString()); 
				System.out.println("\t"+obj.toString()); 
			}
			
			
			System.out.println("\n\nListig all the classes");
			ARTURIResource predType = owlModel.createURIResource("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
			ARTURIResource objClass = owlModel.createURIResource("http://www.w3.org/2000/01/rdf-schema#Class");
			artIter = owlModel.listStatements(NodeFilters.ANY, predType, objClass, true, NodeFilters.ANY);
			List <String>listObj = new ArrayList<String>();
			while(artIter.hasNext()){
				ARTStatement artStatement = artIter.next();
				
				ARTNode subj = artStatement.getSubject();
				String subjString = subj.toString();
				if(listObj.contains(subjString))
					continue;
				listObj.add(subjString);
				
			}
			Iterator <String> iter = listObj.iterator();
			while(iter.hasNext()){
				System.out.println("\n"+iter.next());
			}
			
			System.out.println("Exiting program");
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ModelCreationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ModelAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ModelUpdateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedRDFFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedModelConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnloadableModelConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
